package com.example.databasetest.helper;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;

import androidx.annotation.Nullable;

/**
 * 已经存在一个数据表，在创建一个数据表的时候，将构造方法第四个参数设置为比上一次大就能执行onUpgrade()方法
 * @author liuzhihao
 * @create 2021-07-24 16:12
 */
public class MyDatabaseHelper extends SQLiteOpenHelper {

    // 创建数据库表语句
    private static final String CREATE_TABLE = "create table  Book (" +
            "id integer primary key autoincrement," +
            "author text," +
            "price real," +
            "pages integer," +
            "name text)";

    private static final String CREATE_CATEGORY = "create table Catrgory (" +
            "id integer primary key autoincrement," +
            "category_name text," +
            "category_code integer)";

    private Context mContext;

    // 创建数据库
    public MyDatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
        mContext = context;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
         // 创建表
        db.execSQL(CREATE_TABLE);
        db.execSQL(CREATE_CATEGORY);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("drop table if exists Book");
        db.execSQL("drop table if exists Category");
    }
}
